<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>意达设计</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/member.css"/>

</head>
<body class="body">

<!-- header -->
<div class="pageheader"><a href="javascript:void(0);" onclick="api.closeWin();" class="return"></a><span id='head'>添加新地址</span></div>

<!-- contact -->
<div class="addadrelist addadrelistTop">
	<span>收货人：</span><input type="text" class="txt" name="name">
</div>
<div class="addadrelist">
	<span>联系方式：</span><input type="text" class="txt" name="phone">
</div>
<div class="addadrelist">
	<span>所在地区：</span><input type="text" id="picker" name="city" class="txt"  readonly="readonly" placeholder="请选择地区" style="text-align: right;">
</div>
<div class="addadrelist">
	<span>详细地址：</span><input type="text" class="txt" name="addr" placeholder="街道、楼牌号等">
</div>
<div class="daliebox">
	<div class="daletst">
		<p>设为默认地址</p>
		<span>注：每次下单时会使用该地址</span>
	</div>
	<div class="wcith"><span></span></div>
</div>

<div class="sumeNull"></div>
<input type="button" value="确认新增地址" class="btmadres"  onclick="addedit()" id='foot'></div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/adaptive.js"></script>
<script type="text/javascript" src="../script/tabs.js"></script>

<script type="text/javascript" src="../sjld/picker.min.js"></script>
<script type="text/javascript" src="../sjld/city.js"></script>
<script type="text/javascript" src="../sjld/index.js"></script>

<script type="text/javascript">
    window['adaptive'].desinWidth = 750;
    window['adaptive'].baseFont = 28;
    window['adaptive'].maxWidth = 750;
    window['adaptive'].init();

     apiready = function () {
            $api.fixStatusBar($api.dom('.header'));
            api.addEventListener({
                name: 'keyback'
            }, function(ret, err){
                api.closeWidget();
            });
            
            //api.showProgress();
            var para=api.pageParam;
            var type=0;
            if(para.type==1){
            	type=1;
            	$('#head').html('编辑收货地址');
            	$('#foot').val('保存收货地址');



            	var p={token:token,id:para.id};
	            api.ajax({
	                url: webSiteUrl+'/app/user/getaddr',
	                method: 'post',
	                data: {
	                    
	                    values: p
	                }
	            }, function(ret, err) {

	                if(ret.code==200){
	                	
	                    $("input[name=name]").val(ret.data.name);
			            $("input[name=phone]").val(ret.data.phone);
			            $("input[name=city]").val(ret.data.city);
			            $("input[name=addr]").val(ret.data.addr);
			            if(ret.data.default==1){
			            	$('.wcith').addClass('wcithOn');
			            }else{
							$('.wcith').removeClass('wcithOn');
			            }
	                  
	                 
	                   
	                }else{alert(ret.msg)}
	            });



            	

            }

        }
        function addedit(){
			var para=api.pageParam;
        	if(para.type==1){
        		editaddr(para.id);
        	}else{
        		addaddr();
        	}
        }
        
        function addaddr(){
        	
            var name=$("input[name=name]").val();
            var phone=$("input[name=phone]").val();
            var city=$("input[name=city]").val();
            var addr=$("input[name=addr]").val();
            
            if($('.wcith').hasClass('wcithOn')){
            	var def=1;
            }else{
				var def=0;
            }
            
            if(name==''){
                api.alert({msg:"请输入姓名！"});return;
            }
            if(phone==''){
                api.alert({msg:"请输入手机号！"});return;
            }
            if(city==''){
                api.alert({msg:"请输入城市！"});return;
            }
            if(addr==''){
                api.alert({msg:"请输入详细地址！"});return;
            }

            var p={token:token,name:name,phone:phone,city:city,addr:addr,default:def};
            api.ajax({
                url: webSiteUrl+'/app/user/addaddr',
                method: 'post',
                data: {
                    
                    values: p
                }
            }, function(ret, err) {

                if(ret.code==200){
                    alert(ret.msg);
                  
                 
                    api.openWin({
                        name: 'dealer12Name',
                        url: './myaddr.html',
                        reload: true,
                        pageParam: {
                            name: 'text'
                        }
                    });
                }else{alert(ret.msg)}
            });


        }
        function editaddr(id){
            var name=$("input[name=name]").val();
            var phone=$("input[name=phone]").val();
            var city=$("input[name=city]").val();
            var addr=$("input[name=addr]").val();
            if($('.wcith').hasClass('wcithOn')){
            	var def=1;
            }else{
				var def=0;
            }
            
            if(name==''){
                api.alert({msg:"请输入姓名！"});return;
            }
            if(phone==''){
                api.alert({msg:"请输入手机号！"});return;
            }
            if(city==''){
                api.alert({msg:"请输入城市！"});return;
            }
            if(addr==''){
                api.alert({msg:"请输入详细地址！"});return;
            }

            var p={token:token,name:name,phone:phone,city:city,addr:addr,default:def,id:id};
            api.ajax({
                url: webSiteUrl+'/app/user/editaddr',
                method: 'post',
                data: {
                    
                    values: p
                }
            }, function(ret, err) {

                if(ret.code==200){
                    alert(ret.msg);
                  
                 
                    api.openWin({
                        name: 'dealer12Name',
                        url: './myaddr.html',
                        reload: true,
                        pageParam: {
                            name: 'text'
                        }
                    });
                }else{alert(ret.msg)}
            });


        }
</script>

</html>
